
#include <channelI.hh>

#include <asterisk.h>
#include <asterisk/channel.h>
#include <asterisk/astobj.h>




asterisk::type::basic::channel convert(struct ast_channel* c)
{
	asterisk::type::basic::channel channel;
	
	if(c)	{
	
		channel.name = c->name;
		channel.exten = c->exten;
		
		channel.cdr.src			=	c->cdr->src;
		channel.cdr.dst			=	c->cdr->dst;
		channel.cdr.billsec		= 	c->cdr->billsec;
		channel.cdr.duration	= 	c->cdr->duration;
		
		if(struct ast_channel* bridged = ast_bridged_channel(c))
			channel.bridged		=	bridged->name;
		
		ast_channel_unlock(c);
	}
	
	
	return channel;
}






channelI::channelI()
{
	
}


asterisk::type::basic::channel channelI::getbyname(const std::string& name, const Ice::Current&)
{
	

	return convert(ast_get_channel_by_name_locked(name.c_str()));
}

asterisk::type::basic::channel channelI::getbyexten(const std::string& exten, const Ice::Current&)
{
	return convert(ast_get_channel_by_exten_locked(exten.c_str(),""));
}

asterisk::type::comp::list::channel channelI::get(const Ice::Current&)
{
	struct ast_channel *c = NULL;
	asterisk::type::comp::list::channel cs;
		
	while ((c = ast_channel_walk_locked(c)) != NULL) {
	
		
		cs.push_back(convert(c));
	
		ast_channel_unlock(c);
	}
	
	return cs;
}

